package com.asset.dispoal.plaintiffConfiguration.service.commom;

import com.asset.dispoal.plaintiffConfiguration.domain.db.entity.SysPlaintiffConfiguration;
import com.asset.dispoal.plaintiffConfiguration.domain.db.mapper.SysPlaintiffConfigurationMapper;
import com.asset.dispoal.plaintiffConfiguration.dto.SysPlaintiffConfigurationRequest;
import com.asset.dispoal.plaintiffConfiguration.service.converter.PlaintiffConfigurationConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

/**
 * @author qr
 * @date 2022/3/21$
 */
@Component
public class PlaintiffConfigurationUpdateCommand implements BaseCommand<SysPlaintiffConfigurationRequest, Result> {

    @Autowired
    SysPlaintiffConfigurationMapper sysPlaintiffConfigurationMapper;

    @Override
    public Result execute(SysPlaintiffConfigurationRequest sysPlaintiffConfigurationRequest) {
        SysPlaintiffConfiguration sysPlaintiffConfiguration = PlaintiffConfigurationConverter.INSTANCE.reqToDo(sysPlaintiffConfigurationRequest);
        sysPlaintiffConfigurationMapper.update(sysPlaintiffConfiguration);
        sysPlaintiffConfigurationMapper.deleteCourtByPlaintiffId(sysPlaintiffConfiguration.getId());
        if (CollectionUtil.isNotEmpty(sysPlaintiffConfigurationRequest.getCourtIdList())) {
            for (String courtId : sysPlaintiffConfigurationRequest.getCourtIdList()) {
                sysPlaintiffConfigurationMapper.insertCourt(sysPlaintiffConfiguration.getId(), courtId);
            }
        }
        return Result.ok();
    }
}
